load("//bazel:macros.bzl", "go_binary", "go_library", "py_binary")

package(
    default_applicable_licenses = ["//:license"],
)

licenses(["notice"])

go_library(
    name = "interface_lib",
    srcs = [
        "gen_interface.go",
        "templates.go",
    ],
    importpath = "go.skia.org/skia/tools/ganesh/gl/interface",
    visibility = ["//visibility:private"],
    deps = ["@com_github_flynn_json5//:json5"],
)

go_binary(
    name = "interface",
    embed = [":interface_lib"],
    visibility = ["//visibility:public"],
)

_GENERATE_INTERFACE = """
import os
import subprocess
import sys

# Change into the Skia root directory
# https://bazel.build/docs/user-manual#running-executables
# Note: Bazel eats single quotes, so we must use double quotes.
os.chdir(os.environ["BUILD_WORKSPACE_DIRECTORY"])

# execpath returns the path to the given label relative to the Skia root.
# https://bazel.build/reference/be/make-variables#predefined_label_variables
gen_interface_exe = os.path.abspath("$(execpath :interface)")
interface_json = os.path.abspath("$(execpath interface.json5)")

cmd = [
    gen_interface_exe, "--in_table", interface_json,
    "--out_dir", "src/gpu/ganesh/gl",
]
if "--dryrun" in sys.argv:
    cmd.append("--dryrun")

print(subprocess.check_output(cmd, encoding="utf-8"))
"""

genrule(
    name = "create_generate_gl_interfaces_script",
    # This must be in srcs and not tools because otherwise the path will
    # be resolved incorrectly (as if it were built for the host config
    # and not the exec config).
    srcs = [":interface"],
    outs = ["generate_gl_interfaces.py"],
    cmd = "echo '%s' > $@" % _GENERATE_INTERFACE,
    tools = [
        ":interface.json5",
    ],
)

py_binary(
    name = "generate_gl_interfaces",
    srcs = [":generate_gl_interfaces.py"],
    data = [
        ":interface",
        ":interface.json5",
    ],
    tags = ["no-remote-exec"],
)
